//
//  B_IntegralOrderDetailVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/30.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_IntegralOrderDetailVC.h"
#import "B_OrderHeaderView.h"
#import "B_OrderDetailTVC.h"
#import "B_OrderDetailLabelTVC.h"
#import "B_OrrderFooterView.h"
#import "B_IntegralOrderDetailModel.h"

@interface B_IntegralOrderDetailVC ()
@property (nonatomic, strong) BaseView * mainView;
@property (nonatomic, strong) B_IntegralOrderDetailModel * detailModel;
@end

@implementation B_IntegralOrderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"订单详情"];
    self.view = self.mainView;
    [self getData];
}


/*
 需要根据不同的状态 分别配套底部的按钮状态
 */
- (BaseView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseView alloc] init];
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_OrderDetailTVC" bundle:nil] forCellReuseIdentifier:@"B_OrderDetailTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_OrderDetailLabelTVC" bundle:nil] forCellReuseIdentifier:@"B_OrderDetailLabelTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 1;
            } else {
                if ([weSelf.detailModel.type isEqualToString:@"1"]) {
                    return 1;
                } else if ([weSelf.detailModel.type isEqualToString:@"2"]) {
                    return 3;
                } else if ([weSelf.detailModel.type isEqualToString:@"3"]) {
                    return 3;
                }
                return 0;
            }
        };
        
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_OrderHeaderView * header = [B_OrderHeaderView initB_OrderHeaderView];
                header.B_orderNumber.text = [NSString stringWithFormat:@"订单号: %@", weSelf.detailModel.orderNumber];
                if ([weSelf.detailModel.type isEqualToString:@"1"]) {
                    header.B_orderStatus.text = @"待发货";
                } else if ([weSelf.detailModel.type isEqualToString:@"2"]) {
                    header.B_orderStatus.text = @"已发货";
                } else if ([weSelf.detailModel.type isEqualToString:@"3"]) {
                    header.B_orderStatus.text = @"已完成";
                }
                
                header.B_userName.text = weSelf.detailModel.deliveryName;
                header.B_userPhone.text = weSelf.detailModel.deliveryPhone;
                header.B_userAddress.text = weSelf.detailModel.address;
                header.B_merchantView.hidden = YES;
                return header;
            } else {
                return nil;
            }
        };
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            return nil;
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                B_OrderDetailTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_OrderDetailTVC" forIndexPath:indexPath];
                cell.B_goodsImageView.backgroundColor = [UIColor redColor];
                cell.B_goodsTitle.text = weSelf.detailModel.goodsName;
                cell.B_goodsPrice.text = [NSString stringWithFormat:@"-%@积分", safeValue1(weSelf.detailModel.integralCount)];
                cell.B_goodsCount.hidden = YES;
                return cell;
            } else {
                B_OrderDetailLabelTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_OrderDetailLabelTVC" forIndexPath:indexPath];
                if (indexPath.row == 0) {
                    cell.B_titleLabel.text = [NSString stringWithFormat:@"下单时间: %@", [NSObject getTime:weSelf.detailModel.createTime]];
                } else if (indexPath.row == 1) {
                    cell.B_titleLabel.text = [NSString stringWithFormat:@"发货时间: %@", [NSObject getTime:weSelf.detailModel.deliverTime]];
                } else if (indexPath.row == 2) {
                    cell.B_titleLabel.text = [NSString stringWithFormat:@"快递单号: %@", weSelf.detailModel.trackingNumber];
                }
                return cell;
            }
        };
    }
    return _mainView;
}

- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userIntegral/userIntegralOrderInfo/%@", self.B_orderId] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.detailModel = [B_IntegralOrderDetailModel mj_objectWithKeyValues:data[@"values"]];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
        [weSelf.mainView.tableView reloadData];
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
